package com.bjpowerrnode.streamTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CMap {
    public static void main(String[] args) {
        /**
         * map 方法用于映射每个元素到对应的结果，以下代码片段使用 map 输出了元素对应的平方数：
         * distinct去重
         */
        List<Integer> integers = Arrays.asList(3, 2, 8,2,5,4, 9, 6, 5, 7);
//        List<Integer> list = integers.stream().map(i -> i * i).collect(Collectors.toList());
        List<Integer> list = integers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        list.forEach(System.out::println);
        System.out.println("=======================================================");
        integers.stream().forEach(System.out::println);
        System.out.println("=======================================================");
        Student student1 = new Student(1,"sun");
        Student student2 = new Student(2,"xue");
        Student student3 = new Student(3,"teng");
        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.forEach(System.out::println);
        students.forEach(student -> {
            student.setId(10);
            System.out.println(student);
        });
        Integer integer = students.stream().map(student -> student.getId()).reduce((a, b) -> {
            return a + b;
        }).get();
        System.out.println("integer =" + integer);
        students.forEach(System.out::println);
    }
}

class Student{
    private int id;
    private String name;

    public  Student(){}

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}